# v7
from TreeHelper import *


class Solution:
    def insertNode(self, dic: dict, num: int, deep: int):
      
        dic[deep] = TreeNode(num)
        if(not dic[deep-1].left):
            dic[deep-1].left = dic[deep]
        else:
            dic[deep-1].right = dic[deep]

    def recoverFromPreorder(self, S: str) -> TreeNode:
        deep, num = 0, ""
        dic = {-1: TreeNode(0)}
        for i in range(0, len(S)):
            t = S[i]
            if(t >= '0' and t <= '9'):
                num = num+t
            elif (num):
                self.insertNode(dic, int(num), deep)
                deep, num = 1, ""
            else:
                deep = deep+1
        self.insertNode(dic, int(num), deep)
        return dic[0]


if __name__ == "__main__":
    # [1,2,5,3,4,6,7]
    visitTree(Solution().recoverFromPreorder("1-2--3--4-5--6--7"))
    # [1,2,5,3,null,6,null,4,null,7]
    visitTree(Solution().recoverFromPreorder("1-2--3---4-5--6---7"))
    # [1,2,5,3,null,6,null,4,null,7]
    visitTree(Solution().recoverFromPreorder("1-401--349---90--88"))
    pass
